/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.haw.dbp.a4.playground;

import dc.connectionmanager.database.OracleConnectionManager;
import dc.connectionmanager.logic.IUpdaterConnStatus;
import de.haw.dbp.a4.logic.DBCommand;
import de.haw.global.settings.DatabaseSettings;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 *
 * @author jmenzel
 */
public class SimpleSQLExecuter extends JFrame
{
    private final OracleConnectionManager ocm;
    private final JPanelSimpleSQLExecuter sse;
    private final ArrayList<Component> usedComponents;
    public SimpleSQLExecuter()
    {
        super("Simple SQL Executer");
    
    
        this.setSize(800, 500);
        this.setResizable(false);

        try
        {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception ex)
        {} //Nothing to do here

        this.usedComponents = new ArrayList<Component>();

        ocm = new OracleConnectionManager(DatabaseSettings.ORACLE_URL, DatabaseSettings.ORACLE_PORT, DatabaseSettings.ORACLE_USERNAME, DatabaseSettings.ORACLE_PASSWORD, DatabaseSettings.ORACLE_SID);
        ocm.registerCommandClass(new DBCommand(ocm));
        ocm.setMinConnection(1);
        ocm.setMaxConnection(3);
        ocm.setUpdaterInterval(3000);
        
        sse = new JPanelSimpleSQLExecuter(this, ocm);
        this.add(sse);
        
        ocm.registerConnStatusFunc(new IUpdaterConnStatus() 
        {
                @Override
                public void handleConnectionStatus(boolean connected) 
                {
                        lockGUIElements(connected);
                }
        });
        
        this.addWindowListener(new WindowAdapter() 
		{
			@Override
			public void windowClosing(WindowEvent e) 
			{
				super.windowClosing(e);
				System.exit(0);
			}
		});
        
    }
    
    private void lockGUIElements(boolean connected) 
    {
        for(Component com : this.usedComponents)
        {
                if(com.isEnabled() != connected)
                {
                        com.setEnabled(connected);
                }
        }
    }
    
    public static void main(String args[])
    {
        new SimpleSQLExecuter().setVisible(true);
    }
    
}
